/*
 * MyFormattingToolBar.java
 *
 * Created on May 28, 2006, 1:04 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

/**
 *
 * @author Timoz
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.plaf.*;
import javax.swing.border.*;

public class MyFormattingToolBar extends JToolBar {
    JComboBox comboObjects = new JComboBox();
    JComboBox comboFonts = new JComboBox();
    JComboBox comboFontSize = new JComboBox();
    JToggleButton buttonBold = new JToggleButton(new ImageIcon(getClass().getResource("/Images/Bold.png")));
    JToggleButton buttonItalic = new JToggleButton(new ImageIcon(getClass().getResource("/Images/Italic.png")));
    JToggleButton buttonAlignLeft = new JToggleButton(new ImageIcon(getClass().getResource("/Images/AlignLeft.png")));
    JToggleButton buttonAlignCenter = new JToggleButton(new ImageIcon(getClass().getResource("/Images/AlignCenter.png")));
    JToggleButton buttonAlignRight = new JToggleButton(new ImageIcon(getClass().getResource("/Images/AlignRight.png")));
    JButton buttonBackColor = new JButton(new ImageIcon(getClass().getResource("/Images/BackColor.png")));
    JButton buttonForeColor = new JButton(new ImageIcon(getClass().getResource("/Images/ForeColor.png")));
    JButton buttonLineColor = new JButton(new ImageIcon(getClass().getResource("/Images/LineColor.png")));
    JComboBox comboLineWidth = new JComboBox(new Object[] {"0","1","2","3","4","5","6"});
    MyFrame frame;
    boolean flagPanelSelected;
    private Component [] arrayOfComponents;
    String [] fontSizes = {"8","9","10","11","12","14","16","18","20","22","24","26","28","36","48","72"};
    
    /** Creates a new instance of MyFormattingToolBar */
    public MyFormattingToolBar(MyFrame designFrame) {
        frame=designFrame;
        
        setLayout(new FlowLayout(FlowLayout.LEFT));
        
        // Font info is obtained from the current graphics environment.
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        
        //--- Get an array of font names (smaller than the number of fonts)
        String[] fontNames = ge.getAvailableFontFamilyNames();
        
        //--- Get an array of fonts.  It's preferable to use the names above.
        Font[] allFonts = ge.getAllFonts();
        
        for (int i=0;i<allFonts.length;i++) {
            comboFonts.addItem(allFonts[i].getName());
        }
        
        //----------------------------FontSizes---------------------------
        DefaultComboBoxModel fontSizeModel = new DefaultComboBoxModel(fontSizes);
        comboFontSize.setModel(fontSizeModel);
        comboObjects.setToolTipText("Object");
        comboFonts.setToolTipText("Font");
        comboFontSize.setToolTipText("Font Size");
        buttonBold.setToolTipText("Bold");
        buttonItalic.setToolTipText("Italic");
        buttonAlignLeft.setToolTipText("Align Left");
        buttonAlignCenter.setToolTipText("Align Center");
        buttonAlignRight.setToolTipText("Align Right");
        buttonBackColor.setToolTipText("Fill/Back Color");
        buttonForeColor.setToolTipText("Font/Fore Color");
        buttonLineColor.setToolTipText("Line/Border Color");
        comboLineWidth.setToolTipText("Line/Border Width");
        ButtonGroup buttonGroupAlignment = new ButtonGroup();
        
        buttonGroupAlignment.add(buttonAlignLeft);
        buttonGroupAlignment.add(buttonAlignCenter);
        buttonGroupAlignment.add(buttonAlignRight);
        
        comboObjects.addPopupMenuListener(new ObjectsListHandler());
        comboFonts.addPopupMenuListener(new FontHandler());
        comboFontSize.addPopupMenuListener(new FontSizeHandler());
        
        ButtonHandler buttonHandler = new ButtonHandler();
        buttonAlignLeft.addActionListener(buttonHandler);
        buttonAlignRight.addActionListener(buttonHandler);
        buttonAlignCenter.addActionListener(buttonHandler);
        buttonBold.addActionListener(buttonHandler);
        buttonItalic.addActionListener(buttonHandler);
        
        ColorHandler buttonColorHandler = new ColorHandler();
        buttonBackColor.addActionListener(buttonColorHandler);
        buttonForeColor.addActionListener(buttonColorHandler);
        buttonLineColor.addActionListener(buttonColorHandler);
        
        comboLineWidth.addPopupMenuListener(new LineWidthComboBoxHandler());
        
        add(comboObjects);
        addSeparator();
        add(comboFonts);
        addSeparator();
        add(comboFontSize);
        addSeparator();
        add(buttonBold);
        add(buttonItalic);
        addSeparator();
        add(buttonAlignLeft);
        add(buttonAlignCenter);
        add(buttonAlignRight);
        addSeparator();
        add(buttonBackColor);
        add(buttonForeColor);
        add(buttonLineColor);
        addSeparator();
        add(comboLineWidth);
    }
    
    public void refreshObjects() {
        comboObjects.removeAllItems();
        arrayOfComponents = frame.panel.getComponents();
        comboObjects.addItem("Panel");
        for (int i=0;i<arrayOfComponents.length;i++) {
            if ((arrayOfComponents[i] instanceof JPanel)==false) {
                comboObjects.addItem(arrayOfComponents[i].getName());
            }
        }
        comboObjects.setSelectedItem("Panel");
    }
    
    public void setSelectedComponent(String selectedComponent) {
        comboObjects.setSelectedItem(selectedComponent);
    }
    
    public class ObjectsListHandler implements PopupMenuListener {
        public void popupMenuCanceled(PopupMenuEvent e) {}
        
        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {}
        
        public void popupMenuWillBecomeInvisible(PopupMenuEvent p) {
            if (comboObjects.getSelectedItem()!=null) {
                if (((String)comboObjects.getSelectedItem()).equals("Panel")) {
                    frame.panel.requestFocusInWindow();
                    frame.selectedComponentList.removeAllElements();
                    for (MyComponentContainer c: frame.componentList) {
                        if (c.isChosen()) {
                            c.hideBorder();
                            c.setChosen(false);
                        }
                    }
                    disableButtons();
                    enablePanelButtons();
                } else {
                    for (MyComponentContainer c : frame.componentList) {
                        if (c.getName().equals(comboObjects.getSelectedItem())) {
                            c.setChosen(true);
                            c.showBorder();
                            enableButtons(c);
                            frame.selectedComponentList.add(comboObjects.getSelectedItem());
                        } else {
                            if (c.isChosen()) {
                                c.setChosen(false);
                                c.hideBorder();
                                frame.selectedComponentList.remove(c.getName());
                            }
                        }
                    }
                }
            }
        }
    }
    
    public class LineWidthComboBoxHandler implements PopupMenuListener {
        public void popupMenuCanceled(PopupMenuEvent e) {}
        
        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {}
        
        public void popupMenuWillBecomeInvisible(PopupMenuEvent p) {
            try {
                int lineWidth = Integer.parseInt((String)comboLineWidth.getSelectedItem());
                for (MyComponentContainer c : frame.componentList) {
                    if (c.isChosen()) {
                        if (c.getComponent().getBorder() instanceof LineBorder) {
                            if (c instanceof MyLabelContainer) {
                                if (lineWidth==0) {
                                    c.getComponent().setBorder(BorderFactory.createLineBorder(((LineBorder)c.getComponent().getBorder()).getLineColor(),1));
                                } else {
                                    c.getComponent().setBorder(BorderFactory.createLineBorder(((LineBorder)c.getComponent().getBorder()).getLineColor(),lineWidth));
                                }
                                ((MyLabelContainer)c).setBorderWidth(lineWidth);
                            } else if (c instanceof MyTextFieldContainer && lineWidth==0) {
                                c.getComponent().setBorder(BorderFactory.createLineBorder(((LineBorder)c.getComponent().getBorder()).getLineColor(),1));
                            } else {
                                c.getComponent().setBorder(BorderFactory.createLineBorder(((LineBorder)c.getComponent().getBorder()).getLineColor(),lineWidth));
                            }
                        }
                    }
                }
                frame.flagDirty=true;
                frame.toolbar.toolSave.setEnabled(true);
                frame.handleUndo();
                frame.panel.requestFocusInWindow();
            } catch (Exception ex) {
            }
        }
    }
    
    public class ButtonHandler implements ActionListener {
        public void actionPerformed(ActionEvent a) {
            if (((JToggleButton)a.getSource()).isEnabled()) {
                if (((JToggleButton)a.getSource()).getToolTipText().equals("Bold")) {
                    if (((JToggleButton)a.getSource()).isSelected()) {
                        for (MyComponentContainer c : frame.componentList) {
                            if (c.isChosen()) {
                                if (c.getComponent().getFont().isItalic()) {
                                    c.getComponent().setFont(new Font(c.getComponent().getFont().getName(),Font.BOLD+Font.ITALIC,c.getComponent().getFont().getSize()));
                                } else {
                                    c.getComponent().setFont(new Font(c.getComponent().getFont().getName(),Font.BOLD,c.getComponent().getFont().getSize()));
                                }
                            }
                        }
                    } else {
                        for (MyComponentContainer c : frame.componentList) {
                            if (c.isChosen()) {
                                if (c.getComponent().getFont().isItalic()) {
                                    c.getComponent().setFont(new Font(c.getComponent().getFont().getName(),Font.ITALIC,c.getComponent().getFont().getSize()));
                                } else {
                                    c.getComponent().setFont(new Font(c.getComponent().getFont().getName(),Font.PLAIN,c.getComponent().getFont().getSize()));
                                }
                            }
                        }
                    }
                } else if (((JToggleButton)a.getSource()).getToolTipText().equals("Italic")) {
                    if (((JToggleButton)a.getSource()).isSelected()) {
                        for (MyComponentContainer c : frame.componentList) {
                            if (c.isChosen()) {
                                if (c.getComponent().getFont().isBold()) {
                                    c.getComponent().setFont(new Font(c.getComponent().getFont().getName(),Font.ITALIC+Font.BOLD,c.getComponent().getFont().getSize()));
                                } else {
                                    c.getComponent().setFont(new Font(c.getComponent().getFont().getName(),Font.ITALIC,c.getComponent().getFont().getSize()));
                                }
                            }
                        }
                    } else {
                        for (MyComponentContainer c : frame.componentList) {
                            if (c.isChosen()) {
                                if (c.getComponent().getFont().isBold()) {
                                    c.getComponent().setFont(new Font(c.getComponent().getFont().getName(),Font.BOLD,c.getComponent().getFont().getSize()));
                                } else {
                                    c.getComponent().setFont(new Font(c.getComponent().getFont().getName(),Font.PLAIN,c.getComponent().getFont().getSize()));
                                }
                            }
                        }
                    }
                } else if (((JToggleButton)a.getSource()).getToolTipText().equals("Align Left")) {
                    alignLeft();
                } else if (((JToggleButton)a.getSource()).getToolTipText().equals("Align Right")) {
                    alignRight();
                } else if (((JToggleButton)a.getSource()).getToolTipText().equals("Align Center")) {
                    alignCenter();
                }
            }
            frame.flagDirty=true;
            frame.toolbar.toolSave.setEnabled(true);
            frame.handleUndo();
            frame.panel.requestFocusInWindow();
        }
    }
    
    public class FontSizeHandler implements PopupMenuListener {
        public void popupMenuCanceled(PopupMenuEvent e) {}
        
        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {}
        
        public void popupMenuWillBecomeInvisible(PopupMenuEvent p) {
            for (MyComponentContainer c : frame.componentList) {
                if (c.isChosen()) {
                    c.getComponent().setFont(new Font(c.getComponent().getFont().getName(),c.getComponent().getFont().getStyle(),Integer.parseInt((String)comboFontSize.getSelectedItem())));
                }
            }
            frame.flagDirty=true;
            frame.toolbar.toolSave.setEnabled(true);
            frame.handleUndo();
            frame.panel.requestFocusInWindow();
        }
    }
    
    public class FontHandler implements PopupMenuListener {
        public void popupMenuCanceled(PopupMenuEvent e) {}
        
        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {}
        
        public void popupMenuWillBecomeInvisible(PopupMenuEvent p) {
            for (MyComponentContainer c : frame.componentList) {
                if (c.isChosen()) {
                    c.getComponent().setFont(new Font((String)comboFonts.getSelectedItem(),c.getComponent().getFont().getStyle(),c.getComponent().getFont().getSize()));
                }
            }
            frame.flagDirty=true;
            frame.toolbar.toolSave.setEnabled(true);
            frame.handleUndo();
            frame.panel.requestFocusInWindow();
        }
    }
    
    public class ColorHandler implements ActionListener {
        public void actionPerformed(ActionEvent a) {
            if (((JButton)a.getSource()).isEnabled()) {
                Color color;
                if (((JButton)a.getSource()).getToolTipText().equals("Fill/Back Color")) {
                    JColorChooser backColorChooser=new JColorChooser();
                    color = backColorChooser.showDialog(frame, "Fill/BackGround Color", null);
                    if (color != null) {
                        if (flagPanelSelected) {
                            frame.panel.setBackground(color);
                        } else {
                            for (MyComponentContainer c : frame.componentList) {
                                if (c.isChosen()) {
                                    c.getComponent().setBackground(color);
                                }
                            }
                        }
                    }
                }else if (((JButton)a.getSource()).getToolTipText().equals("Font/Fore Color")) {
                    JColorChooser foreColorChooser=new JColorChooser();
                    color = foreColorChooser.showDialog(frame, "Font/ForeGround Color", null);
                    if (color != null) {
                        for (MyComponentContainer c : frame.componentList) {
                            if (c.isChosen()) {
                                c.getComponent().setForeground(color);
                            }
                        }
                    }
                }else if (((JButton)a.getSource()).getToolTipText().equals("Line/Border Color")) {
                    JColorChooser lineColorChooser=new JColorChooser();
                    color = lineColorChooser.showDialog(frame, "Line/Border Color", null);
                    if (color != null) {
                        for (MyComponentContainer c : frame.componentList) {
                            if (c.isChosen()) {
                                if (c.getComponent().getBorder() instanceof LineBorder) {
                                    c.getComponent().setBorder(BorderFactory.createLineBorder(color,((LineBorder)c.getComponent().getBorder()).getThickness()));
                                }
                            }
                        }
                    }
                }
                frame.flagDirty=true;
                frame.toolbar.toolSave.setEnabled(true);
                frame.handleUndo();
                frame.panel.requestFocusInWindow();
            }
        }
    }
    
    public void enableButtons(MyComponentContainer component) {
        flagPanelSelected=false;
        comboFonts.setEnabled(true);
        comboFontSize.setEnabled(true);
        buttonBold.setEnabled(true);
        buttonItalic.setEnabled(true);
        buttonAlignLeft.setEnabled(true);
        buttonAlignCenter.setEnabled(true);
        buttonAlignRight.setEnabled(true);
        buttonBackColor.setEnabled(true);
        buttonForeColor.setEnabled(true);
        buttonLineColor.setEnabled(true);
        comboLineWidth.setEnabled(true);
        
        if (component != null) {
            comboFonts.setSelectedItem(component.getComponent().getFont().getName());
            comboFontSize.setSelectedItem(Integer.toString(component.getComponent().getFont().getSize()));
            if (component.getComponent().getBorder() instanceof LineBorder) {
                if (component instanceof MyLabelContainer) {
                    comboLineWidth.setSelectedItem(Integer.toString(((MyLabelContainer)component).getBorderWidth()));
                } else {
                    comboLineWidth.setSelectedItem(Integer.toString(((LineBorder)component.getComponent().getBorder()).getThickness()));
                }
            }
            if (component.getComponent().getFont().isBold()) {
                buttonBold.setSelected(true);
            } else {
                buttonBold.setSelected(false);
            }
            if (component.getComponent().getFont().isItalic()) {
                buttonItalic.setSelected(true);
            } else {
                buttonItalic.setSelected(false);
            }
            if (component.getComponent() instanceof JLabel) {
                int alignment = ((JLabel)component.getComponent()).getHorizontalAlignment();
                if (alignment==JLabel.LEFT) {
                    buttonAlignLeft.setSelected(true);
                } else if (alignment==JLabel.RIGHT) {
                    buttonAlignRight.setSelected(true);
                } else if (alignment==JLabel.CENTER){
                    buttonAlignCenter.setSelected(true);
                } else {
                    buttonAlignCenter.setSelected(false);
                    buttonAlignRight.setSelected(false);
                    buttonAlignLeft.setSelected(false);
                }
            }
            if (component.getComponent() instanceof JTextField) {
                int alignment = ((JTextField)component.getComponent()).getHorizontalAlignment();
                if (alignment==JTextField.LEFT) {
                    buttonAlignLeft.setSelected(true);
                } else if (alignment==JTextField.RIGHT) {
                    buttonAlignRight.setSelected(true);
                } else if (alignment==JLabel.CENTER){
                    buttonAlignCenter.setSelected(true);
                } else {
                    buttonAlignCenter.setSelected(false);
                    buttonAlignRight.setSelected(false);
                    buttonAlignLeft.setSelected(false);
                }
            }
            if (component.getComponent() instanceof JButton) {
                int alignment = ((JButton)component.getComponent()).getHorizontalAlignment();
                if (alignment==JButton.LEFT) {
                    buttonAlignLeft.setSelected(true);
                } else if (alignment==JButton.RIGHT) {
                    buttonAlignRight.setSelected(true);
                } else if (alignment==JLabel.CENTER){
                    buttonAlignCenter.setSelected(true);
                } else {
                    buttonAlignCenter.setSelected(false);
                    buttonAlignRight.setSelected(false);
                    buttonAlignLeft.setSelected(false);
                }
            }
        } else {
            buttonBold.setSelected(false);
            buttonItalic.setSelected(false);
            buttonAlignLeft.setSelected(false);
            buttonAlignRight.setSelected(false);
            buttonAlignCenter.setSelected(false);
            comboLineWidth.setSelectedItem(null);
        }
    }
    
    public void disableButtons() {
        comboFonts.setEnabled(false);
        comboFontSize.setEnabled(false);
        buttonBold.setEnabled(false);
        buttonItalic.setEnabled(false);
        buttonAlignLeft.setEnabled(false);
        buttonAlignCenter.setEnabled(false);
        buttonAlignRight.setEnabled(false);
        buttonForeColor.setEnabled(false);
        buttonLineColor.setEnabled(false);
        comboLineWidth.setEnabled(false);
        flagPanelSelected=true;
    }
    
    public void enablePanelButtons() {
        flagPanelSelected=true;
        buttonBackColor.setEnabled(true);
    }
    
    public void alignLeft() {
        for (MyComponentContainer c : frame.componentList) {
            if (c.isChosen()) {
                if (c.getComponent() instanceof JLabel) {
                    ((JLabel)c.getComponent()).setHorizontalAlignment(JLabel.LEFT);
                } else if (c.getComponent() instanceof JTextField) {
                    ((JTextField)c.getComponent()).setHorizontalAlignment(JLabel.LEFT);
                }
            }
        }
    }
    
    public void alignRight() {
        for (MyComponentContainer c : frame.componentList) {
            if (c.isChosen()) {
                if (c.getComponent() instanceof JLabel) {
                    ((JLabel)c.getComponent()).setHorizontalAlignment(JLabel.RIGHT);
                } else if (c.getComponent() instanceof JTextField) {
                    ((JTextField)c.getComponent()).setHorizontalAlignment(JLabel.RIGHT);
                }
            }
        }
    }
    
    public void alignCenter() {
        for (MyComponentContainer c : frame.componentList) {
            if (c.isChosen()) {
                if (c.getComponent() instanceof JLabel) {
                    ((JLabel)c.getComponent()).setHorizontalAlignment(JLabel.CENTER);
                } else if (c.getComponent() instanceof JTextField) {
                    ((JTextField)c.getComponent()).setHorizontalAlignment(JLabel.CENTER);
                }
            }
        }
    }
}
